package study.alex.resumedb.gae;

import com.vaadin.data.util.converter.Converter;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * Created with IntelliJ IDEA.
 * User: alex
 * Date: 28.01.14
 * Time: 21:13
 * SVN $Id$
 */
public class DateConverter implements Converter<Date, GregorianCalendar> {
    @Override
    public GregorianCalendar convertToModel(Date value, Class<? extends GregorianCalendar> targetType, Locale locale) throws ConversionException {
        if (value != null) {
            GregorianCalendar gc = new GregorianCalendar(locale);
            gc.setTime(value);
            return gc;
        } else return null;
    }

    @Override
    public Date convertToPresentation(GregorianCalendar value, Class<? extends Date> targetType, Locale locale) throws ConversionException {
        return value == null ? null : new Date(value.getTimeInMillis());
    }

    @Override
    public Class<GregorianCalendar> getModelType() {
        return GregorianCalendar.class;
    }

    @Override
    public Class<Date> getPresentationType() {
        return Date.class;
    }
}
